package com.gm.controller;
import com.gm.entity.User;
import com.gm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
    @Autowired
    UserMapper userMapper;

    @PostMapping("/user/login")
    public String doLogin(@RequestParam("username") String username , @RequestParam("password") String password , Model model , HttpSession session){
        User user =this.userMapper.getUserByName(username);
        if (user != null){
            if (user.getPassword().equals(password)){
                session.setAttribute("userInfo",username);
                return "main";
            }
            else {
                model.addAttribute("msg","密码错误！");
                return "/login";
            }
        }
        model.addAttribute("msg","用户名不存在！");
        return "/login";
    }
}
